<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
class UsersController extends Controller
{
   // 前台用户管理 
    public function getIndex(Request $request){
        if(empty(session('id'))){
            return redirect('/admin')->with('error','请您登陆');
        }
         // 分页
        $count = $request -> input('count',10);
         // 关键字
        $search = $request -> input('search','');
        // 对查询语句构造器进行分页  paginate  查询数据并且分页
        $data = DB::table('user') ->where('username','like','%'.$search.'%')-> paginate($count);
     return view('admin/users/index ',['data'=>$data,'count'=>$count,'search'=>$search,'request'=>$request->all()]);
    }
    // 添加用户页
    public function getAdd(){
        return view('admin/users/add');
    }
    // 添加用户
    public function postInsert(Request $request){
        // 自动表单验证
        $this->validate($request,[
            'username' => 'required',
            'password' => 'required',
            'repassword' => 'required|same:password',
            'email' =>'email',
            
             ],[
             'username.required' => '请填写同户名',
             'password.required' => '请输入密码',
             'repassword.required' => '请输入确认密码',
             'repassword.same' => '确认密码不一致',
             'email.email' => '请输入邮箱正确格式',
              ]);
        // 获取数据
        $res = $request -> except('_token','repassword');
           if($request -> hasFile('upic')){
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('upic') -> getClientOriginalExtension();
            // 拼接文件名
            $name = $temp_name.'.'.$hz;
            $request -> file('upic') -> move('./upload',$name);
             $res['upic'] = $name;  
        }
        // 通过邮箱 查看用户是否存在
       $y =  DB::table('user') ->where('email',$res['email']) -> orwhere('username',$res['username'])-> first();
        if(!empty($y)){
            return back() -> with('error','此用户名或邮箱已存在');
        }else{
            // 用户不存在  
            // 对密码进行哈希加密
            $res['password'] = Hash::make($res['password']);
           $data = DB::table('user') -> insert($res);
            if($data){
                return redirect('/admin/users/index')->with('success','添加成功');
            }else{
                return back() -> with('error','添加失败');
            }
        }
    } 
    //用户删除
    public function getDelete($id){
        $data = DB::table('user') ->where('id',$id)-> delete(); 
       if($data = 1){
       return redirect('/admin/users/index')->with('success','删除成功');
       }else{
        return back() -> with('error','删除失败');
       }
    }
    // 用户修改页 
    public function getEdit($id){
        // 通过id 获取所有数据
        $data = DB::table('user') ->where('id',$id)-> first();
        return view('admin/users/edit',['data'=>$data]);
    }
    // 用户修改
    public function postUpdate(Request $request){
          // 进行自动表单验证  表单值存在闪存中
           $this->validate($request, [
             'username' => 'required',
            'password' => 'required',
           'email' =>'email',
             ],[
             'username.required' => '请填写同户名',
             'password.required' => '请输入密码',
             'repassword.same' => '确认密码不一致',
             'email.email' => '请输入邮箱正确格式',
           
        ]);
        $arr = $request -> except(['_token','id']);
      
        // 获取id时
        $id = $request -> input('id');

          if($request -> hasFile('upic')){
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('upic') -> getClientOriginalExtension();
            // 拼接文件名
            $name = $temp_name.'.'.$hz;
            $request -> file('upic') -> move('./upload',$name);
             $arr['upic'] = $name;  
        }
        
       
        
        $data = DB::table('user') ->where('id',$id)-> update($arr);
          if($data = 1){
            
            return redirect('/admin/users/index') -> with('success','修改成功');
       }else{
            return back() -> with('error','修改失败');
       } 
    }
    
    
}
